package demo1;

import java.lang.reflect.Array;
import java.util.Arrays;

class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //return this.age-o.age;//从小到大
        return o.age-this.age;//从大到小
        //return this.name.compareTo(o.name);
    }
}
public class Text {

    public static void main(String[] args) {

    }
    public static void main3(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student("zhangsan",10);
        students[1]=new Student("lisi",19);
        students[2]=new Student("abc",8);
        System.out.println("排序前:"+ Arrays.toString(students));
        //Arrays.sort(students);
        //mysort(students);
        Arrays.sort(students);
        System.out.println("排序后:"+Arrays.toString(students));
        System.out.println("排序前:"+ Arrays.toString(students));
        NameComparator nameComparator=new NameComparator();
        Arrays.sort(students,nameComparator);
        System.out.println("排序后:"+Arrays.toString(students));
        AgeComparator ageComparator=new AgeComparator();
        Arrays.sort(students,ageComparator);
        System.out.println("排序后:"+Arrays.toString(students));
    }
    public static void mysort(Comparable[] comparables){
        for (int i = 0; i < comparables.length - 1; i++) {
            for (int j = 0; j < comparables.length - 1 - i; j++) {
                if(comparables[j].compareTo(comparables[j+1])>0){
                    Comparable tmp=comparables[j];
                    comparables[j]=comparables[j+1];
                    comparables[j+1]=tmp;
                }
            }
        }
    }
    public static void main2(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student("zhangsan",10);
        students[1]=new Student("lisi",19);
        students[2]=new Student("abc",8);
        System.out.println("排序前:"+ Arrays.toString(students));
        //Arrays.sort(students);
        //mysort(students);
        Arrays.sort(students);
        System.out.println("排序后:"+Arrays.toString(students));
    }
    public static void main1(String[] args) {
        Student student1=new Student("zhangsan",10);
        Student student2=new Student("lisi",19);
        //System.out.println(student1.compareTo(student2));
    }
}
